﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Controls;
using VAdvantage.Classes;

namespace VAdvantage.Grids
{
    public class VDataGridFileColumn : DataGridBoundColumn, IDataGridColumn
    {
        private TextBlock textBlock = null;
        private GridField _field = null;
        private String _columnName = "";

        private VFile editingControl = null;
        private object currentCellValue;

        
        public string DataPropertyName
        {
            get
            {
                return _columnName;
            }
            set
            {
                _columnName = value.ToUpper();
                this.Binding = new System.Windows.Data.Binding(_columnName);
            }
        }

        public Controls.VDataGrid DataGridView
        {
            get;
            set;
        }

        public GridField Field
        {
            get
            {
                return _field;
            }
            set
            {
                _field = value;

                
           }
        }

        public void Dispose()
        {
            if (editingControl != null)
            {
                editingControl.Dispose();
            }
            textBlock = null;
            editingControl = null;
            _field = null;
        }

        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            if (editingControl == null)
            {
                editingControl = new VFile(_field.GetColumnName(), _field.IsMandatory(false), _field.IsReadOnly(), _field.IsEditable(false), _field.GetDisplayType() == DisplayType.FileName);
                editingControl.SetField(_field);
            }

            currentCellValue = ((DataUtil.DataObject)dataItem).GetFieldValue(_columnName);

            textBlock = (TextBlock)cell.Content;

            editingControl.SetValue(currentCellValue);
            return editingControl;
        }


        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            if (cell.Content is VFile)
            {
                editingControl.VetoableChangeListener -= new EventHandler(editingControl_VetoableChangeListener);

                currentCellValue = ((VFile)cell.Content).GetValue();
                _field.SetValue(currentCellValue);
                ((DataUtil.DataObject)dataItem).SetFieldValue(_columnName, currentCellValue);
                //currentBlock.Text = value == null ? "0" : value.ToString();
                //textBlock.Text = currentCellValue == null ? "" : currentCellValue.ToString();
                return textBlock;
            }

            TextBlock tb = Utility.AEnv.GetDataGridElement();

            if (Binding != null)
            {
                tb.SetBinding(TextBlock.TextProperty, Binding);
            }
            return tb;
           
        }

        protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
        {
            //if (editingEventArgs.OriginalSource is TextBlock)
            //{
               // textBlock = (TextBlock)editingEventArgs.OriginalSource;
            //}


            editingControl.VetoableChangeListener += new EventHandler(editingControl_VetoableChangeListener);
            return editingElement;
        }

        void editingControl_VetoableChangeListener(object sender, EventArgs e)
        {
            DataGridView.RaiseCellValueChanged(sender);
            editingControl.VetoableChangeListener -= new EventHandler(editingControl_VetoableChangeListener);
        }

        public bool ReadOnly
        {

            get;
            set;

        }

    }
}
